import ResMgr from "../Managers/ResMgr";
import GameResPkg from "./GameResPkg";
import SceneManager from "../Managers/SceneManager"
import DataManager from "../Managers/DataManager";


const {ccclass, property} = cc._decorator;



export default class GameApp extends cc.Component {

    public static Instance: GameApp = null;

    onLoad(): void {
        if(GameApp.Instance === null) {
            GameApp.Instance = this;
        }
        else {
            this.destroy();
            return;
        }
    }



    public GameStart(): void 
    {
        var loading = cc.find("Canvas/ResLoading");
        var proBar = loading.getChildByName("UIProgress").getChildByName("value").getComponent(cc.Sprite);
        proBar.fillRange = 0;

        // var resPkg = {
        //     "Sounds": [
        //         { assetType: cc.AudioClip, urls: 
        //             [   "HGS", 
        //                 "Shot"
        //         ]},
        //     ]
        
        //     // "Sounds": AudioClip,
        // }


        ResMgr.Instance.preloadResPkg(GameResPkg, (now, total)=> {
            proBar.fillRange = now / total;
        }, ()=>{
            this.scheduleOnce(()=>{
                loading.destroy();
                this.enterLoginScene();
            }, 0.5);
        });
        

    }

    
    
    public enterLoginScene(): void 
    {
        // var canvas = cc.find("Canvas");
        // var level1Prefab = ResMgr.Instance.getAsset("Maps", "Level1");
        // var level1 = cc.instantiate(level1Prefab);
        // canvas.addChild(level1);
        // level1.y += 160;

        // var playerPrefab = ResMgr.Instance.getAsset("Charactors", "Player_1");
        // var player = cc.instantiate(playerPrefab);
        // canvas.addChild(player);
        // player.setPosition(cc.v2(0, 0));
        
        // var loginUIPrefab = ResMgr.Instance.getAsset("GUI", "ui_prefabs/GameUI");
        // var loginUi = cc.instantiate(loginUIPrefab);
        // canvas.addChild(loginUi);
        
        // var clip = ResMgr.Instance.getAsset("Sounds", "HGS");
        // cc.audioEngine.playEffect(clip, true);
        
        // UIMgr.Instance.remove_ui("GameUI");


        // var loginUI:cc.Node = UIMgr.Instance.show_ui("LoginUI");
        // loginUI.addComponent("LoginUI_Ctrl");


        // var txt = ResMgr.Instance.getAsset("Txt", "level1");
        // DataManager.handleData("level1",txt.text.split('\n'))
        // var ooxx = DataManager.GetTableByID("level1",1);
        

        // var txt = ResMgr.Instance.getAsset("Txt", "level");
        // DataManager.handleData("level",txt.text.split('\n'))
        // var ooxx = DataManager.GetTableByID("level",1);

        cc.assetManager.loadBundle("picList", (err: Error, bundle: cc.AssetManager.Bundle) => 
        {
            bundle.load("1301_a",cc.SpriteAtlas,function(err, assets: cc.SpriteAtlas)
            {
                var ooxx:cc.SpriteAtlas = bundle.get("1301_a",cc.SpriteAtlas);
                cc.log("ssss");
            });
            // bundle.load("xxx");
        });

        // var  testPlist: cc.SpriteAtlas = ResMgr.Instance.getAsset("picList", "1301_a",cc.SpriteAtlas);
        // var oppxx:cc.SpriteFrame = testPlist.getSpriteFrame("zhfsgj-01");


        // var scene:cc.Node = SceneManager.getInstace().change_scene("LoginScene");
        // scene.addComponent("LoginScene");


    }






}
